package cn.tiga.basic.service.impl;

import cn.tiga.basic.mapper.BaseMapper;
import cn.tiga.basic.query.BaseQuery;
import cn.tiga.basic.service.IBaseService;
import cn.tiga.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
// @Service 这个类不要交给spring管理，因为本类只用于让子类继承代码而已，不做真正的业务执行
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    
    @Autowired
    private BaseMapper<T> baseMapper;

    /**
     * @Title: loadAll
     * @Description: 查询所有部门数据
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/6/21 16:40  
     * @Parameters: []
     * @Return java.util.List<cn.itsource.org.domain.T>
     */
    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    /**
     * @Title: save
     * @Description: TODO
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/6/21 16:41
     * @Parameters: [t]
     * @Return void
     */
    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public PageList<T> queryData(BaseQuery baseQuery) {

        // 1.查询分页总条数
        Integer total = baseMapper.queryCount(baseQuery);

        // 2.查询当前页数据
        List<T> rows = baseMapper.queryData(baseQuery);

        return new PageList<>(total, rows);
    }

    /**
     * @Title: batchDel
     * @Description: 批量删除
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/6/25 9:48
     * @Parameters: [ids]
     * @Return void
     */
    @Override
    @Transactional
    public void batchDel(Long[] ids) {
        baseMapper.batchDel(ids);
    }
    
}
